<?php
/**
 * 应用系统用户管理
 * @author tangbl
 *
 */
class AdminAction extends CommonAction {
 
    /**
     * 初始化
     */
    public function _initialize(){
    
    	parent::_initialize();
    	//parent::checkTimestamp();
    }
    
    /**
     * 根据对应的条件查询用户
     * @access public
     * @param  int    $id  用户id
     * @return array
     */
    public function find(){
    
    	$id = $this->_get("id","intval",0);
    
    	$model = D("Admin");
    	$map   = array("id"=>$id);
    	$data  = $model->where($map)->find();
    
    	$this->success($data);
    }
    
    /**
     * 设置用户的状态
     * @access public
     * @param  int    $id      用户id
     * @param  int    $status  用户状态
     * @return array
     */
    public function active(){
   
    	$id     = $this->_get("id"    ,"intval",0);
    	$status = $this->_get("status","intval",0);
    
    	$model = D("Admin");
    	$data  = $model->find($id);
    	
    	if($data['admin'] == 2){
    		$map = array("id"=>$id, "status"=>$status);
    		$model->save($map);
    		$this->success("变更状态成功");
    	}else{
    		$this->error("管理员不能变更状态");
    	}
    }
    
    /**
     * 添加用户
     * @access public
     * @param  int    $company  所属商户
     * @param  int    $outletid 所属门店
     * @return array
     */
    public function add(){
    	
    	parent::do_add("Admin");
    }
    
    /**
     * 修改用户
     * @access public
	 * @param  int    $company  所属商户
     * @param  int    $outletid 所属门店
     * @return array
     */
    public function edit(){
    	
    	$param = array(
    		"before"  => "admin_edit_before",
    	);
    	parent::do_edit("Admin", $param);
    }
    
    /**
     * 查询用户分页
     * @access public
     * @param  int    $company 所属商户
     * @return array
     */
    public function page(){
		
    	$company = $this->_get("company","intval",0);
    	$role    = $this->_get("role"   ,"intval",0);
    	if($company){
    		$map['company'] = $company;
    	}
    	if($role){
    		$map['role'] = $role;
    	}
    	    	
    	$desc    = $this->_get("orderDirection","trim","desc");;
		$order   = $this->_get("orderName","trim","addtime");
		$pageNum = $this->_get("pageNum","intval",C('PAGE_NUMS'));
		$nowPage = $this->_get("p","intval",1);
    	
    	$model = D("Admin");
    	$count = $model->where($map)->count();
    	$list  = $model->where($map)->order($order.' '.$desc)
    			 ->page($nowPage.','.$pageNum)->select();
    	//echo $model->getLastSql();exit;
    	
    	$data  = array("count"=>$count, "list"=>$list);
    	$this->success($data);
    }
    
    /**
     * 用户列表
     * @access public
     * @param  int    $company  所属商户
     * @param  int    $outletid 所属门店
     * @param  int    $role     所属角色
     * @return array
     */
    public function index(){
    
    	$company  = $this->_req("company" ,"intval",0);
    	$role     = $this->_req("role"    ,"intval",0);
    	$outletid = $this->_req("outletid","intval",0);
    	
    	if($company){
    		$map['company'] = $company;
    	}
    	if($role){
    		$map['role'] = $role;
    	}
    	if($outletid){
    		$map['outletid'] = $outletid;
    	}
    	 
    	$model = D("Admin");
    	$list  = $model->where($map)->select();
    	 
    	$this->success($list);
    }
    
    /**
     * 根据应用系统角色查询对应的节点
     * @access public
     * @param  int    $role 角色id
     * @return array
     */
    public function listByRole(){
    
    	$map['role'] = $this->_get("role","intval",0);
    
    	$model = D("NodeRoleView");
    	$data  = $model->where($map)->select();
    	 
    	$this->success($data);
    }
    
    /**
     * 删除用户
     * @access public
     * @param  int    $id  节点编号
     * @return array
     */
    public function del(){
    	
    	$id     = $this->_get("id","intval",0);
    	
    	$model  = D("Admin");
    	$result = $model->delAdmin($id);
    	
    	if($result){
    		$this->success($result,"删除角色成功");
    	}else{
    		$this->error("删除角色失败");
    	}
    }
    
    /**
     * 查询用户列表
     * @access public
     * @param  int    $company 所属商户
     * @return array
     */
    public function userList(){
    
    	$company  = $this->_req("company" ,"intval",0);
    	$role     = $this->_req("role"    ,"intval",0);
    	$outletid = $this->_req("outletid","intval",0);
    	if($company){
    		$map['company'] = $company;
    	}
    	if($role){
    		$map['role'] = $role;
    	}
    	if($outletid){
    		$map['outletid'] = $outletid;
    	}
    	
    	$model = D("Admin");
    	$list  = $model->where($map)->select();
    	$this->success($list);
    }
    
    /**
     * 更改用户桌面背景
     * @access public
     * @param  int    $userid  用户编号
     * @param  int    $company 所属商户
     * @param  String $desktop 桌面背景
     * @return array
     */
    public function desktop(){
    	 
    	$map['id']      = $this->_req("userid" ,"intval",0);
    	$map['company'] = $this->_req("company","intval",0);
    	
    	$model = D("Admin");
    	$rows  = $model->where($map)->find();
    	
    	if($rows['id']){
    		
    		$data['desktop'] = $this->_req("desktop","trim","");
    		$result = $model->where($map)->save($data);
    		
    		if($result){
    			$this->success($result);
    		}else{
    			$this->error();
    		}
    	}else{
    		$this->error();
    	}
    }
    
    
}